package com.rong.mbt.personal.activity;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TextAppearanceSpan;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.rong.mbt.personal.R;
import com.rong.mbt.personal.bean.PersonalInfoBean;
import com.rong.mbt.personal.dialog.CustomerServiceDialog;
import com.rong.mbt.personal.utils.ImageUtil;
import com.rong.mbt.personal.utils.StringUtil;

public class MonkeyPackageActivity extends PBaseActivity {


    private PersonalInfoBean infoBean;
    private String mbtToken;
    ImageView ivHeadProtrait;
    private TextView tvName;
    private TextView tvMoney;
    private ImageView ivCs;
    private ImageView ivSetting;
    private RelativeLayout rtRecharge;
    private RelativeLayout rtWithdrawa;

    @Override
    public int getLayoutId() {
        return R.layout.activity_monkey_package;
    }

    @Override
    public void initView() {
        super.initView();
        mbtToken = getIntent().getStringExtra("mbtToken");
        init();
        listener();
    }

    @Override
    public void onStart() {
        super.onStart();
        initNet();
    }
    private void initNet() {
        OkGo.<String>get("http://service.kantingyong.com/user/getUser")
                .tag(this)
                .headers("mbtToken", mbtToken)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        Log.i("test", "onSuccess getUser  " + response.body());
                        if (!response.body().isEmpty()) {
                            infoBean = JSON.parseObject(response.body(), PersonalInfoBean.class);
                            if (infoBean == null || infoBean.getData() == null) {
                                return;
                            }
                            if (infoBean.getData().getHeadimage() != null) {
                                ImageUtil.loadCircleImage(MonkeyPackageActivity.this, infoBean.getData().getHeadimage(), ivHeadProtrait);
                            }

                            tvName.setText(infoBean.getData().getName());

                            SpannableString spannableString = null;
                            if (infoBean.getData().getUsablemoney() != null) {
                                String str = StringUtil.formatString(Double.parseDouble(infoBean.getData().getUsablemoney()));
                                spannableString = new SpannableString(str);
                            } else {
                                spannableString = new SpannableString("0.00");
                            }
                            spannableString.setSpan(new TextAppearanceSpan(MonkeyPackageActivity.this, R.style.dp_20), spannableString.length() - 3, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            tvMoney.setText(spannableString);
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        Log.i("test", "onError  " + response.body());
                    }
                });
    }
    public void init() {
        ivHeadProtrait = findViewById(R.id.fragment_personal_iv_head_portrait);
        tvName = findViewById(R.id.head_tv_name);
        tvMoney = findViewById(R.id.tv_money);
        ivCs = findViewById(R.id.head_iv_kf);
        ivSetting = findViewById(R.id.iv_setting);

        rtRecharge = findViewById(R.id.fragment_personal_relative_recharge);
        rtWithdrawa = findViewById(R.id.fragment_personal_relative_withdrawal);
    }
    private void listener() {
        ivSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MonkeyPackageActivity.this, PersonalInfoActivity.class);
                if (infoBean != null) {
                    intent.putExtra("infoBean", infoBean);
                    intent.putExtra("mbtToken", mbtToken);
                }
                startActivity(intent);
            }
        });


        ivCs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CustomerServiceDialog csDialog = new CustomerServiceDialog(MonkeyPackageActivity.this);
                csDialog.show();
            }
        });

        //充值
        rtRecharge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MonkeyPackageActivity.this, RechargeActivity.class);
                intent.putExtra("mbtToken", mbtToken);
                startActivity(intent);
            }
        });
        //提现
        rtWithdrawa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MonkeyPackageActivity.this, WithdrawalActivity.class);
                intent.putExtra("mbtToken", mbtToken);
                startActivity(intent);
            }
        });
        //账单
        findViewById(R.id.fragment_personal_line_bill).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MonkeyPackageActivity.this, BillActivity.class);
                intent.putExtra("mbtToken", mbtToken);
                startActivity(intent);
            }
        });

        //二维码
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (infoBean == null || infoBean.getData() == null) {
                    return;
                }
                Intent intent = new Intent(MonkeyPackageActivity.this, MyQRcodeActivity.class);
                intent.putExtra("infoBean", infoBean);
                intent.putExtra("mbtToken", mbtToken);
                startActivity(intent);
            }
        });
        findViewById(R.id.fragment_personal_iv_qrcode).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (infoBean == null || infoBean.getData() == null) {
                    return;
                }
                Intent intent = new Intent(MonkeyPackageActivity.this, MyQRcodeActivity.class);
                intent.putExtra("infoBean", infoBean);
                intent.putExtra("mbtToken", mbtToken);
                startActivity(intent);
            }
        });
    }
}
